package com.caiwen.service;

import com.caiwen.blog.pojo.TbSeller;
import com.caiwen.blog.service.sellerLogin.SellerLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private SellerLoginService sellerLoginService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        TbSeller seller = sellerLoginService.findSellerByTelephone(username);

        if (seller != null){
            return new User(username,seller.getPassword(),grantAuths());
        }

        return null;
    }

    private List<GrantedAuthority> grantAuths(){
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("ROLE_SELLER"));

        return list;
    }
}
